package com.lemon.exam.common.converter;

import io.r2dbc.spi.ColumnMetadata;
import io.r2dbc.spi.Row;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.lang.NonNull;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类对象
 *
 * @author Lemon
 * @since 2025/9/2 16:32
 */
@ReadingConverter
public class MapConverter implements Converter<Row, Map<String, Object>> {
    @Override
    @NonNull
    @SuppressWarnings("ConstantConditions")
    public Map<String, Object> convert(Row source) {
        List<? extends ColumnMetadata> metas = source.getMetadata().getColumnMetadatas();
        if (metas.isEmpty()) return Collections.emptyMap();
        Map<String, Object> ret = new HashMap<>(metas.size());
        for (ColumnMetadata meta : metas) {
            ret.put(meta.getName(), source.get(meta.getName(), meta.getJavaType()));
        }
        return ret;
    }
}
